<?php

namespace app\admin\controller;


use app\common\controller\Backend;
use think\Exception;

class Wechat extends Backend
{
    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['index'];

    public function index()
    {
        $amount = $this->request->get('amount');
        if ($amount < 1) {
            $this->error('至少充1元钱吧!');
        }
        $params['admin_id'] = $this->request->get('admin_id') == 'undefined' ? $this->auth->id : $this->request->get('admin_id');
        $params['pay_price'] = $amount;
        $params['money'] = $amount;
        $params['pay_type'] = 'wechat';
        $params['status'] = 'create';
        $params['out_trade_no'] = date("YmdHis") . mt_rand(100000, 999999);
        $params['create_time'] = time();
        $result = \app\admin\model\Recharge::insert($params);
        if ($result) {
//            $order = [
//                'out_trade_no' => $params['out_trade_no'],
////                'total_fee' => $params['pay_price'] * 100, // **单位：分**
//                'total_fee' => 1, // **单位：分**
//                'body' => '商家充值',
//            ];
            $order = [
                'amount' => $params['pay_price'],
//                'amount' => 0.01,
                'orderid' => $params['out_trade_no'],
                'type' => "wechat",
                'title' => "自助充值",
                'method' => $this->request->isMobile() ? 'wap' : 'scan',
            ];

            try {
                $url = \addons\epay\library\Service::submitOrder($order);
            } catch (Exception $e) {
                $this->error($e->getMessage());
            }
            if (!$this->request->isMobile()) {
                $this->redirect($this->request->domain() . '/qrcode/build?text=' . $url);
                exit;
            }
        } else {
            $this->error('添加充值记录失败!');
        }
        return;
    }
}